package com.ggy.ggyaiagent.agent.model;

import com.ggy.ggyaiagent.advisor.MyLoggerAdvisor;
import com.ggy.ggyaiagent.chatmemory.RedisChatMemory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class GGYManus extends ToolCallAgent {

    public GGYManus(ToolCallbackProvider provider,
                    ToolCallback[] allTools, ChatModel dashscopeChatModel,
                    RedisTemplate<String,Object> redisTemplate
            ) {
        super(provider,allTools);
        this.setName("GGYManus");
        this.setMaxSteps(20);
        String SYSTEM_PROMPT = """  
                You are YuManus, an all-capable AI assistant, aimed at solving any task presented by the user.  
                You have various tools at your disposal that you can call upon to efficiently complete complex requests.  
                """+"！attention： The maximum number of steps for this project is only "+this.getMaxSteps()+". Try to complete the task with the fewest steps to prevent excessive consumption of AI."
                +"If the user's question is too simple, end the answer as soon as possible.";
        this.setSystemPrompt(SYSTEM_PROMPT);
        String NEXT_STEP_PROMPT = """  
                Based on user needs, proactively select the most appropriate tool or combination of tools.  
                For complex tasks, you can break down the problem and use different tools step by step to solve it.  
                After using each tool, clearly explain the execution results and suggest the next steps.  
                If you want to stop the interaction at any point, use the `terminate` tool/function call.  
                """;
        this.setNextStepPrompt(NEXT_STEP_PROMPT);
//基于redis的对话记忆
//        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
//                .chatMemoryRepository(new RedisChatMemory(redisTemplate))
//                .maxMessages(22)
//                .build();
        //初始化客户端
        ChatClient build = ChatClient.builder(dashscopeChatModel)
                .defaultAdvisors(
//                        //上下文回答的保存机制
//                        MessageChatMemoryAdvisor.builder(chatMemory).build(),
                        new MyLoggerAdvisor())
                .build();
        this.setChatClient(build);
    }
}
